/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager;

import Golf_Manager.sqlite.MemberDAO;
import Golf_Manager.sqlite.sql.MemberSql;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.DefaultListSelectionModel;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
/**
 *
 * @author Administrator
 */
public class MemberListDialog extends javax.swing.JDialog {

    /**
     * Creates new form NewOkCancelDialog
     */
    public MemberListDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();

        // Close the dialog when Esc is pressed
        String cancelName = "cancel";
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put(cancelName, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                    member = null;
                    setVisible(false);
                    dispose();
            }
        });
        
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JTextField.CENTER);
        jTableMemberListTable.getColumnModel().getColumn(0).setCellRenderer(renderer);
        jTableMemberListTable.getColumnModel().getColumn(1).setCellRenderer(renderer);
        
        jTableMemberListTable.getTableHeader().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int col = jTableMemberListTable.columnAtPoint(e.getPoint());
                TableColumnModel cmodel = jTableMemberListTable.getColumnModel();

                for(int headColIndex = 0; headColIndex < cmodel.getColumnCount(); headColIndex++){
                    if(col != headColIndex)
                    cmodel.getColumn(headColIndex).setHeaderValue(cmodel.getColumn(headColIndex).getHeaderValue().toString().replace("▲", "").replace("▼", ""));
                }

                if(cmodel.getColumn(col).getHeaderValue().toString().contains("▲")) {
                    //order가 ASC(▲)인 경우 DESC(▼)
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().replace("▲", "▼"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" DESC"));
                } else if(cmodel.getColumn(col).getHeaderValue().toString().contains("▼"))  {
                    //order가 DESC(▼)인 경우 ASC(▲)
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().replace("▼", "▲"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" ASC"));
                } else {
                    //order가 없는경우
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().concat("▲"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" ASC"));
                }
                //jTableMemberListTable.getTableHeader().repaint();
            }
        });
        
        jTableMemberListTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JTable target = (JTable)e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();
                    // do some action
                    //System.out.println("row :" + row + ", column :" + column);
                    Golf_Manager.model.MemberDialogModel memberDialogModel =  (Golf_Manager.model.MemberDialogModel)jTableMemberListTable.getModel();
                    member = memberDialogModel.getMember(row);
                    setVisible(false);
                    dispose();
                }
            }
        });
        
        meeberOrderSearch("");
    }

    public Golf_Manager.vo.Member getMember() {
        return member;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMemberListTable = new javax.swing.JTable();
        searchName = new javax.swing.JTextField();

        setResizable(false);

        okButton.setText("조회");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("이름");

        jTableMemberListTable.setModel(new Golf_Manager.model.MemberDialogModel());
        jTableMemberListTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableMemberListTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableMemberListTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchName, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton)
                    .addComponent(searchName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                .addContainerGap())
        );

        getRootPane().setDefaultButton(okButton);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        TableColumnModel cmodel = jTableMemberListTable.getColumnModel();
        for(int headColIndex = 0; headColIndex < cmodel.getColumnCount(); headColIndex++){
            cmodel.getColumn(headColIndex).setHeaderValue(cmodel.getColumn(headColIndex).getHeaderValue().toString().replace("▲", "").replace("▼", ""));
        }
        meeberOrderSearch("");
    }//GEN-LAST:event_okButtonActionPerformed
    
    private void meeberOrderSearch(String orderString){ 
        Golf_Manager.model.MemberDialogModel memberDialogModel =  (Golf_Manager.model.MemberDialogModel)jTableMemberListTable.getModel();
        DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) jTableMemberListTable.getSelectionModel();
        
        StringBuilder whrerSql = new StringBuilder("AND JOINYN = '참여'");
        if(searchName.getText() != null && !searchName.getText().equals("")){
            whrerSql.append(" AND NAME LIKE '%").append(searchName.getText()).append("%'");
        } 
        memberDialogModel.setMembers(MemberDAO.selectMember(whrerSql, orderString));
        jTableMemberListTable.getTableHeader().repaint();
        jTableMemberListTable.repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMemberListTable;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField searchName;
    // End of variables declaration//GEN-END:variables

    private Golf_Manager.vo.Member member = null;
}
